<template>
    <div>
        <!-- 面包屑导航栏区域-->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>角色管理</el-breadcrumb-item>
            <el-breadcrumb-item>角色列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片内容区域-->
        <el-card>
            <!-- 添加角色按钮区域-->
            <el-row>
                <el-col>
                    <el-button type="primary">添加角色</el-button>
                </el-col>
            </el-row>

            <!-- 角色列表区域-->
            <el-table
                    :data="roles"
                    style="width: 100%" border stripe>
                <!-- 展开列 角色权限-->
                <el-table-column type="expand" width="70">
                    <el-table-column type="expand">
                        <template v-slot="scope">
                            <el-row :class="['bdbottom','vcenter', i1 === 0 ? 'bdtop' : '']"
                                    v-for="(item, i1) in scope.row.children" :key="item.id">
                                <!-- 渲染一级权限-->
                                <el-col :span="5">
                                    <el-tag @close="removePri(scope.row, item.id)" closable>{{item.authName}}</el-tag>
                                    <i class="el-icon-caret-right"/>
                                </el-col>
                                <!--渲染二级和三级权限-->
                                <el-col :span="19">
                                    <el-row :class="[i2===0?'':'bdtop', 'vcenter']" v-for="(item2, i2) in item.children"
                                            :key="item2.id">
                                        <!-- 渲染二级权限-->
                                        <el-col :span="6">
                                            <el-tag type="success" closable @close="removePri(scope.row, item2.id)">
                                                {{item2.authName}}
                                            </el-tag>
                                            <i class="el-icon-caret-right"/>
                                        </el-col>
                                        <!-- 渲染三级权限 -->
                                        <el-col :span="18">
                                            <el-tag type="warning" v-for="(item3, i3) in item2.children" closable
                                                    :key="item3.id" @close="removePri(scope.row, item3.id)">
                                                {{item3.authName}}
                                            </el-tag>
                                        </el-col>
                                    </el-row>
                                </el-col>
                            </el-row>
                        </template>
                    </el-table-column>
                </el-table-column>

                <!-- 数据表格区域-->
                <el-table-column type="index" label="ID" width="70"></el-table-column>
                <el-table-column prop="roleName" label="角色名称" width="180"></el-table-column>
                <el-table-column prop="roleDesc" label="角色描述" width="180"></el-table-column>
                <el-table-column label="操作" width="300">
                    <template v-slot="scope">
                        <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
                        <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
                        <el-button type="warning" icon="el-icon-setting" size="mini" @click="queryRightsTree(scope.row)">分配权限
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
        <!-- 分配权限弹出框-->
        <el-dialog
                title="提示"
                :visible.sync="dialogVisible"
                width="30%">
            <!-- 树形结构展示-->
            <el-tree
                    class="filter-tree"
                    :data="rightsList"
                    :props="rightsProps"
                    default-expand-all
                    :show-checkbox="true"
                    node-key="id"
                    :default-checked-keys="defKeys"
                    ref="rightTreeRef">
            </el-tree>

            <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="saveRight">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "roles",
        data() {
            return {
                roles: [],
                dialogVisible: false,
                // 展示树形结构中那些数据
                rightsProps: {
                    // 指定子节点的名称
                    children: 'children',
                    // 指定标签名称
                    label: 'authName',
                },
                // 树形结构中默认选中
                defKeys:[],
                rightsList: [],
                // 点击分配权限时,保存角色id,供后面使用
                roleId: '',
            }
        },
        created() {
            this.initRoles();
        },
        methods: {
            // 初始化角色列表
            async initRoles() {
                const {data: res} = await this.$http.get(`roles`);
                this.roles = res.data;
            },
            async removePri(role, id) {
                const res = await this.$confirm('是否删除该权限?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).catch(err => err);
                // 用户点击确认返回的是 confirm
                // 用户点击取消 cancel
                // console.log(res);
                this.$message.closeAll();
                if (res !== 'confirm') return this.$message.info("取消删除");
                const {data: r} = await this.$http.delete(`roles/${role.id}/rights/${id}`)
                if (r.meta.status !== 200) return this.$message.error("删除失败");
                // 重新赋值, 就不会刷新页面了
                role.children = r.data;
            },
            // 查询权限树
            async queryRightsTree(role) {
                this.roleId = role.id;
                const {data: res} = await this.$http.get(`rights/tree`)
                this.dialogVisible = true
                if(res.meta.status !== 200)return this.$message.error(res.meta.msg)
                this.rightsList = res.data;
                this.defKeys = [];
                this.getLeafKeys(role, this.defKeys);

            },
            // 遍历获取该角色下的所有权限,传给defKeys
            getLeafKeys(node, arr){
                // 如果当前节点已经是最后子节点
                if(!node.children){
                    return arr.push(node.id);
                }
                // 递归遍历
                node.children.forEach(item => this.getLeafKeys(item, arr));
            },
            // 点击确定,保存用户分配的权限
            async saveRight(){
                const rightIds = [
                    // 获取当前树中,被选中的节点id
                    ...this.$refs.rightTreeRef.getCheckedKeys(),
                    // 获取当前树中,被半选中的节点id
                    ...this.$refs.rightTreeRef.getHalfCheckedKeys()
                ]
                // 拼接id
                const ids = rightIds.join(',');

                const {data : res } = await this.$http.post(`roles/${this.roleId}/rights`, {rids: ids})

                if(res.meta.status !== 200)return this.$message.error(res.meta.msg);
                this.$message.success(res.meta.msg);
                this.initRoles();
                this.dialogVisible = false
            }
        }
    }
</script>

<style lang="less" scoped>
    .bdtop {
        border-top: 1px solid #eee;
    }

    .bdbottom {
        border-bottom: 1px solid;
    }

    .el-tag {
        margin: 7px;
    }

    .vcenter {
        display: flex;
        align-items: center;
    }

</style>